package com.js.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.js.mapper.AttendanceMapper;
import com.js.pojo.Attendance;
import com.js.pojo.Course;
import com.js.pojo.Attendance;
import com.js.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@RequestMapping("attendance")
@Controller
public class AttendanceController {
    @Autowired
    private AttendanceMapper attendanceMapper;
    /***
     * 分页
     * @param model
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping("/queryAttendanceByPage")
    public String queryAttendanceByPage(Model model,
                                    @RequestParam(required = true,defaultValue = "1") Integer page,
                                    @RequestParam(required = false,defaultValue = "4")Integer pageSize){
        //page默认值是1，pageSize默认是10，意思是从第1页开始，每页显示10条记录。
        PageHelper.startPage(page,pageSize);
        List<Attendance> attendances = attendanceMapper.queryAllAttendance();
        PageInfo<Attendance> coursePageInfo = new PageInfo<>(attendances);
        model.addAttribute("page",coursePageInfo);
        return "attendance/attendanceList";
    }
    //跳转到添加考勤的页面
    @RequestMapping("/toAdd")
    public String toAddAttendance(){
        return "attendance/addAttendance";
    }
    @RequestMapping("/toAdd1")
    public String toAddAttendance1(){
        return "student/studentAttendance";
    }
    //添加考勤的功能请求
    @RequestMapping("/add")
    public String addAttendance(Attendance attendance){
        System.out.println("addAttendance==>"+attendance.toString());
        int i = attendanceMapper.addAttendance(attendance);
        if (i>0){
            return "redirect:/attendance/queryAttendanceByPage";//添加考勤后需要，重定向到查询全部考勤
        }
        return "redirect:/attendance/toAdd";
    }
    @RequestMapping("/add1")
    public String addAttendance1(Attendance attendance){
        System.out.println("addAttendance==>"+attendance.toString());
        int i = attendanceMapper.addAttendance(attendance);
        if (i>0){
            return "student/attendanceSuccess";//添加考勤后需要，重定向到查询全部考勤
        }
        return "redirect:/attendance/toAdd1";
    }
    @RequestMapping("/dele/{id}")
    public String deleteAttendanceById(@PathVariable("id") int id){
        int i = attendanceMapper.deleteAttendanceById(id);
        System.out.println(i);
        if (i>0){
            System.out.println("删除成功！！");
        }
        return "redirect:/attendance/queryAttendanceByPage";
    }
    @RequestMapping("/toUpdate")
    public String toUpdateAttendance(int id, Model model){
        Attendance attendance = attendanceMapper.queryAttendanceById(id);
        model.addAttribute("QAttendance",attendance);
        return "attendance/updateAttendance";
    }
    //添加考勤的功能请求
    @RequestMapping("/update")
    public String updateStudent(Attendance attendance, Model model){
        System.out.println("updateStudent==>"+attendance.toString());
        int i = attendanceMapper.updateAttendance(attendance);
        if (i>0){
            return "redirect:/attendance/queryAttendanceByPage";//添加考勤后需要，重定向到查询全部考勤
        }
        return "redirect:/attendance/toUpdate";
    }
    //查询
    @RequestMapping("/queryName")
    public String queryStudentByName(String attendanceName, Model model){
        System.out.println("要查询的考勤:" + attendanceName);
        List<Attendance> attendances = attendanceMapper.queryAttendanceByName(attendanceName);
        PageHelper.startPage(1,4);
        PageInfo<Attendance> pageInfo = new PageInfo<>(attendances);
        model.addAttribute("page",pageInfo);
        return "attendance/attendanceList";
    }
}
